﻿using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyTuyenSinh.BussinessLayer;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmStudents : Office2007Form
    {
        public int load = 0;
        public int loadStydy = 0;
        public int loadStydyMatinh = 0;
        public int loadStydyMatinh10 = 0;
        public int loadStydyMatinh11 = 0;
        public int loadStydyMatinh12 = 0;
        public int loadStydyMatruong10 = 0;
        public int loadStydyMatruong11 = 0;
        public int loadStydyMatruong12 = 0;
        public int loadStydyNV = 0;
        private int ma;

        public frmStudents()
        {
            InitializeComponent();
        }

        private void HideControl(bool hien)
        {
            cmdThem.Visible = hien;
            cmdLuu.Visible = hien;
            cmdXoa.Visible = hien;
            cmdThem.Enabled = true;
            cmdXoa.Enabled = true;
        }

        private void FrmHoSoTsLoad(object sender, EventArgs e)
        {
            try
            {
                #region Load Trường Đại học, cao đẳng

                DataTable listUniversity = Operator.Operator.GetListUniversity();
                DataTable listCD = Operator.Operator.GetListUniversity();
                if (listUniversity == null) return;
                if (listUniversity.Rows.Count > 0)
                {
                    load = 0;
                    cbKHtruongDH.DataSource = listUniversity;
                    cbKHtruongDH.ValueMember = "Id";
                    cbKHtruongDH.DisplayMember = "UniversityDisplay";
                }
                load = 1;
                if (listUniversity.Rows.Count > 1)
                {
                    cbKHtruongDH.SelectedIndex = 1;
                    cbKHtruongDH.SelectedIndex = 0;
                }

                if (listCD == null) return;
                if (listCD.Rows.Count > 0)
                {
                    cbTruongCD.DataSource = listCD;
                    cbTruongCD.ValueMember = "Id";
                    cbTruongCD.DisplayMember = "UniversityDisplay";
                }
                if (listCD.Rows.Count > 1)
                {
                    cbTruongCD.SelectedIndex = 1;
                    cbTruongCD.SelectedIndex = 0;
                }

                #endregion

                cbGioiTinh.SelectedIndex = 1;

                #region Load dân tộc

                DataTable listEthnic = Operator.Operator.GetListEthnic();
                if (listEthnic == null) return;
                if (listEthnic.Rows.Count > 0)
                {
                    cbDanToc.DataSource = listEthnic;
                    cbDanToc.ValueMember = "EthnicId";
                    cbDanToc.DisplayMember = "EthnicName";
                }

                #endregion

                #region Load ưu tiên

                DataTable listPriority = Operator.Operator.GetListPriority();
                if (listPriority == null) return;
                if (listPriority.Rows.Count > 0)
                {
                    cbDoiTuong.DataSource = listPriority;
                    cbDoiTuong.ValueMember = "Id";
                    cbDoiTuong.DisplayMember = "PriorityDisplay";
                }

                #endregion

                #region Load mã tỉnh, mã tỉnh 10, 11,12

                DataTable listProvince = Operator.Operator.GetListProvince();
                DataTable list1 = Operator.Operator.GetListProvince();
                DataTable list2 = Operator.Operator.GetListProvince();
                DataTable list3 = Operator.Operator.GetListProvince();

                if (listProvince.Rows.Count > 0)
                {
                    loadStydyMatinh = 0;
                    cbMaTinh.DataSource = listProvince;
                    cbMaTinh.ValueMember = "Id";
                    cbMaTinh.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh = 1;
                    if (cbMaTinh.Items.Count > 1)
                    {
                        cbMaTinh.SelectedIndex = 1;
                        cbMaTinh.SelectedIndex = 0;
                    }
                }


                if (list1.Rows.Count > 0)
                {
                    loadStydyMatinh10 = 0;
                    cbMaTinh10.DataSource = list1;
                    cbMaTinh10.ValueMember = "Id";
                    cbMaTinh10.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh10 = 1;
                    if (cbMaTinh10.Items.Count > 1)
                    {
                        cbMaTinh10.SelectedIndex = 1;
                        cbMaTinh10.SelectedIndex = 0;
                    }
                }

                if (list2.Rows.Count > 0)
                {
                    loadStydyMatinh11 = 0;
                    cbMaTinh11.DataSource = list2;
                    cbMaTinh11.ValueMember = "Id";
                    cbMaTinh11.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh11 = 1;
                    if (cbMaTinh11.Items.Count > 1)
                    {
                        cbMaTinh11.SelectedIndex = 1;
                        cbMaTinh11.SelectedIndex = 0;
                    }
                }


                if (list3.Rows.Count > 0)
                {
                    loadStydyMatinh12 = 0;
                    cbMaTinh12.DataSource = list3;
                    cbMaTinh12.ValueMember = "Id";
                    cbMaTinh12.DisplayMember = "ProvinceDisplay";
                    loadStydyMatinh12 = 1;
                    if (cbMaTinh12.Items.Count > 1)
                    {
                        cbMaTinh12.SelectedIndex = 1;
                        cbMaTinh12.SelectedIndex = 0;
                    }
                }

                #endregion

                cbNamTN.SelectedIndex = 1;
                CbNamTNCD.SelectedIndex = 1;

                #region Load khu vực

                DataTable listZone = Operator.Operator.GetListZone();
                if (listZone == null) return;
                if (listZone.Rows.Count > 0)
                {
                    cbKhuVuc.DataSource = listZone;
                    cbKhuVuc.ValueMember = "Id";
                    cbKhuVuc.DisplayMember = "ZoneDisplay";
                }

                #endregion

                HideControl(false);
                cmdThem.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

   private void CbNganhSelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydy == 0) return;
            cbKhoi.Items.Clear();
            cbKhoi.Text = "";
            txtTennganh.Text = "";
            if ((cbNganh.SelectedValue != null) && (cbKHtruongDH.SelectedValue != null))
            {
                var study = new Study();
                study.UniversityId = (int) cbKHtruongDH.SelectedValue;
                study.Id = (int) cbNganh.SelectedValue;
                DataTable list = Operator.Operator.GetListStudyByIdUniversityId(study.UniversityId, study.Id);
                if (list.Rows.Count > 0)
                {
                    txtTennganh.Text = list.Rows[0][3].ToString().Trim();
                    txtKhoiThi.Text = list.Rows[0][4].ToString().Trim();
                    string[] a = txtKhoiThi.Text.Split(',');
                    for (int i = 0; i < a.Length; i++)
                    {
                        cbKhoi.Items.Add(a[i]);
                    }
                }
            }
        }

     
        private void CmdThemClick(object sender, EventArgs e)
        {
            HideControl(true);
            ma = 0;
            cmdXoa.Visible = false;
        }

        private void GrHoSoTsCellClick(object sender, DataGridViewCellEventArgs e)
        {
            HideControl(true);
        }

        private void FrmStudentsKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && cmdLuu.Visible)
            {
                CmdLuuClick(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                CmdThemClick(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (cmdXoa.Visible))
            {
                CmdXoaClick(sender, e);
            }
        }

        private void CmdLuuClick(object sender, EventArgs e)
        {
        }

        private void CmdXoaClick(object sender, EventArgs e)
        {
        }


        private void CbKHtruongDhSelectedIndexChanged(object sender, EventArgs e)
        {
            if (load == 0) return;
            if (cbKHtruongDH.SelectedValue != null)
            {
                cbNganh.Text = "";
                txtTentruongDH.Text = "";
                var university = new University();
                university.Id = int.Parse(cbKHtruongDH.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListUniversityById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtTentruongDH.Text = listUniversity.Rows[0][2].ToString().Trim();
                }
                DataTable list = Operator.Operator.GetListStudyByUniversityId(university.Id);
                if (list.Rows.Count > 0)
                {
                    cbNganh.DataSource = list;
                    cbNganh.ValueMember = "Id";
                    cbNganh.DisplayMember = "StudyDisplay";
                    loadStydy = 0;
                }
                if (cbNganh.Items.Count <= 1) return;
                cbNganh.SelectedIndex = 1;
                loadStydy = 1;
                cbNganh.SelectedIndex = 0;
            }
        }



        private void CbMaTinhSelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh == 0) return;
            if (cbMaTinh.SelectedValue != null)
            {
                cbMaHuyen.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListDistrictByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaHuyen.DataSource = listUniversity;
                    cbMaHuyen.ValueMember = "Id";
                    cbMaHuyen.DisplayMember = "DistrictDisplay";
                }
                if (cbMaHuyen.Items.Count <= 1) return;
                cbMaHuyen.SelectedIndex = 1;
                cbMaHuyen.SelectedIndex = 0;
            }
        }

        private void CbMaTinh10SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh10 == 0) return;
            if (cbMaTinh10.SelectedValue != null)
            {
                cbMaTruong10.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh10.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaTruong10.DataSource = listUniversity;
                    cbMaTruong10.ValueMember = "Id";
                    cbMaTruong10.DisplayMember = "HighSchoolDisplay";
                    loadStydyMatruong10 = 0;
                }
                loadStydyMatruong10 = 1;
                if (cbMaTruong10.Items.Count > 1)
                {
                    cbMaTruong10.SelectedIndex = 1;
                    cbMaTruong10.SelectedIndex = 0;
                }
            }
        }

        private void CbMaTinh11SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh11 == 0) return;
            if (cbMaTinh11.SelectedValue != null)
            {
                cbMaTruong11.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh11.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaTruong11.DataSource = listUniversity;
                    cbMaTruong11.ValueMember = "Id";
                    cbMaTruong11.DisplayMember = "HighSchoolDisplay";
                    loadStydyMatruong11 = 0;
                }
                loadStydyMatruong11 = 1;
                if (cbMaTruong11.Items.Count <= 1) return;

                cbMaTruong11.SelectedIndex = 1;
                cbMaTruong11.SelectedIndex = 0;
            }
        }

        private void CbMaTinh12SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatinh12 == 0) return;
            if (cbMaTinh12.SelectedValue != null)
            {
                cbMaTruong12.Text = "";
                var university = new District();
                university.Id = int.Parse(cbMaTinh12.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolByProvinceId(university.Id);
                if (listUniversity.Rows.Count <= 0) return;
                if (listUniversity.Rows.Count > 0)
                {
                    cbMaTruong12.DataSource = listUniversity;
                    cbMaTruong12.ValueMember = "Id";
                    cbMaTruong12.DisplayMember = "HighSchoolDisplay";
                    loadStydyMatruong12 = 0;
                }
                loadStydyMatruong12 = 1;
                if (cbMaTruong12.Items.Count <= 1) return;

                cbMaTruong12.SelectedIndex = 1;
                cbMaTruong12.SelectedIndex = 0;
            }
        }

        private void CbMaTruong10SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatruong10 == 0) return;
            if (cbMaTruong10.SelectedValue != null)
            {
                txtLop10.Text = "";
                var university = new HighSchool();
                university.Id = int.Parse(cbMaTruong10.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtLop10.Text = listUniversity.Rows[0][4].ToString().Trim();
                }
            }
        }

        private void CbMaTruong11SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatruong11 == 0) return;
            if (cbMaTruong11.SelectedValue != null)
            {
                txtLop11.Text = "";
                var university = new HighSchool();
                university.Id = int.Parse(cbMaTruong11.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtLop11.Text = listUniversity.Rows[0][4].ToString().Trim();
                }
            }
        }

        private void CbMaTruong12SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadStydyMatruong12 == 0) return;
            if (cbMaTruong12.SelectedValue != null)
            {
                txtLop12.Text = "";
                var university = new HighSchool();
                university.Id = int.Parse(cbMaTruong12.SelectedValue.ToString());
                DataTable listUniversity = Operator.Operator.GetListHighSchoolById(university.Id);
                if (listUniversity.Rows.Count > 0)
                {
                    txtLop12.Text = listUniversity.Rows[0][4].ToString().Trim();
                }
            }
        }
    }
}